package com.bf.electroplating.config.exception;

import com.bf.electroplating.pojo.vo.ResultVO;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    // 处理 @Validated 注解在方法参数上校验失败抛出的 MethodArgumentNotValidException 异常
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultVO<String> handleMethodArgumentNotValidException(
            MethodArgumentNotValidException ex) {
        return ResultVO.fail(ex.getBindingResult().getAllErrors().stream().findFirst().orElse(new ObjectError("", "系统异常")).getDefaultMessage());
    }

    @ExceptionHandler(IdempotentException.class)
    public ResultVO<String> handleIdempotentException(IdempotentException ex) {
        return ResultVO.fail(ex.getMessage());
    }

}
